package de.jonasjelten.jcalc.mathExps;

import de.jonasjelten.jcalc.exception.JEvaluationException;
import de.jonasjelten.jcalc.math.JNumber;
import java.math.BigInteger;

/**
 *
 * @author michael & jonas
 */
public class JSum extends JMultiArgMathExp {

    public JSum(JMathExp[] args) {
        super(args);
    }

    public JSum(JMathExp arg1, JMathExp arg2) {
        this(new JMathExp[]{arg1, arg2});
    }

    @Override
    public JNumber eval() throws JEvaluationException {
        JNumber[] childrenEvaluated = super.evalChildren();
        super.jcalc.expgroup.addActive();
        JNumber result = new JNumber(BigInteger.ZERO);
        for (int i = 0; i < args.length; i++) {
            result = result.sum(childrenEvaluated[i]);
        }
        super.jcalc.expgroup.setInactive();
        return result;
    }

    @Override
    public String toString() {
        return super.toString(true, "+");
    }

    @Override
    public boolean isTrivial() {
        return false;
    }
}
